
import org.xml.sax.InputSource;
import org.w3c.dom.*;
import javax.xml.xpath.*;
import java.io.*;
import java.net.*;

public class ParseXML 
{
  static float lng = 0;
  static float lat = 0;

  public float getLong ()
  {
    return lng;
  }
  
  public float getLat ()
  {
    return lat;
  }

//  public static void main (String[] args)
  public ParseXML (String addr)
  //public static void main(String[] args) throws IOException
  {
    
//    System.err.println ("called=================");
    
    URL url;
    try
    {
      url = new URL(
        "http://maps.googleapis.com/maps/api/geocode/xml?address=" + addr + "&sensor=true");
//      "http://maps.googleapis.com/maps/api/geocode/xml?address=7+Ave+and+Charles+Street,+New+York,+NY&sensor=true");
    } catch (MalformedURLException e)
    {
      System.err.println ("URL creation error");
      return;
    }

    URLConnection yc;
    try
    {
      yc = url.openConnection();
    } catch (IOException e)
    {
      System.err.println ("URL openConnection error");
      return;
    }
    
    XPathFactory factory = XPathFactory.newInstance();

    XPath xpath = factory.newXPath();

    try
    {
      // The xpath evaluator requires the XML be in the format of an InputSource
      InputSource inputXml = new InputSource ();
      try
      {
        inputXml = new InputSource(yc.getInputStream ());
      } catch (IOException e)
      {
        System.err.println ("inputsource error");
        return;
      }

      // Because the evaluator may return multiple entries, we specify that the expression
      // return a NODESET and place the result in a NodeList.
      NodeList geocodeResp = (NodeList) xpath.evaluate("GeocodeResponse", inputXml, XPathConstants.NODESET);
      
//      System.err.println (geocodeResp.getLength());
      
      
      for (int res_idx = 0; res_idx < geocodeResp.getLength(); res_idx ++)
      {
        Node rootNode = geocodeResp.item (res_idx);
        
        if (rootNode.getNodeName().equals("GeocodeResponse"))
        {
          NodeList nodes = rootNode.getChildNodes();
          
          // We can then iterate over the NodeList and extract the content via getTextContent().
          // NOTE: this will only return text for element nodes at the returned context.
          for (int i = 0; i < nodes.getLength(); i++) 
          {
//            System.err.println (nodes.item(i).getNodeName());
            Node result = nodes.item(i);
            
            if (result.getNodeName().equals("result"))
            {
//              System.err.println("11111");
//              System.err.println (nodes.item(i).getNodeName());
              
              NodeList resultList = nodes.item(i).getChildNodes();
              
              for (int j = 0; j < resultList.getLength(); j ++)
              {
                Node geometry = resultList.item(j);
                
                if (geometry.getNodeName().equals("geometry"))
                {
//                  System.err.println("2222222");
//                  System.err.println (geometry.getNodeName());
                  
                  NodeList geoChildList = geometry.getChildNodes();
                  
                  for (int k = 0; k < geoChildList.getLength(); k++)
                  {
                    Node location = geoChildList.item(k);
                    if (location.getNodeName().equals("location"))
                    {
//                      System.err.println("333333");
//                      System.err.println (location.getNodeName());
                      
                      NodeList longlatList = location.getChildNodes();
                      
                      for (int lli = 0; lli < longlatList.getLength(); lli ++)
                      {
                        Node longlat = longlatList.item(lli);
                        if (longlat.getNodeName().equals("lat"))
                        {
                          String text = longlat.getTextContent();
//                          System.err.println("longitude: " + text.trim());
                          
                          lng = Float.parseFloat(text);
                        }
                        else if (longlat.getNodeName().equals("lng"))
                        {
                          String text = longlat.getTextContent();
//                          System.err.println("latitude: " + text.trim());
                          
                          lat = Float.parseFloat(text);
                        }
                        
                      } // for longlatList
                      
                    } // if location
                  }  // for geoChildList
                }
              }
            } // for
          }
        } // if finds GeocodeResponse
        
        
      }
    }
    catch (XPathExpressionException ex)
    {
      System.out.print("XPath Error");
    }
    
    
    
  }
  
  
  
}

